package tech.aistar.corejava.day09.Equals;

/**
 * Created by 李雨昂 on 2019/7/26.
 */
public class RewriteEquals {

    private int id;

    private String sno;

    private int age;

    public RewriteEquals() {
    }

    public RewriteEquals(int id) {
        this.id = id;
    }

    public RewriteEquals(int id, String sno, int age) {
        this.id = id;
        this.sno = sno;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getSno() {
        return sno;
    }

    public void setSno(String sno) {
        this.sno = sno;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("RewriteEquals{");
        sb.append("id=").append(id);
        sb.append(", sno='").append(sno).append('\'');
        sb.append(", age=").append(age);
        sb.append('}');
        return sb.toString();
    }
    /**
     * 手动重写equals()方法
     */
    /*@Override
    public boolean equals(Object obj){

        //非空性
        if(obj == null){
            return false;
        }

        //自反性
        if(this == obj){
            return true;
        }

        //一致性
        //如果不进行类型转换判断可能 - java.lang.ClassCastException - 类型转换异常
        if(obj instanceof RewriteEquals){//进行类型转换判断
            RewriteEquals rw = (RewriteEquals) obj;//obj没有getId方法要强值类型转换子类RewriteEquals类型
            if(this.id == rw.getId()){
                return true;
            }
        }

        return false;

    }*/
    /**
     * 自动生成equals()和hashCode()方法
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        RewriteEquals that = (RewriteEquals) o;

        if (id != that.id) return false;
        if (age != that.age) return false;
        return sno != null ? sno.equals(that.sno) : that.sno == null;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (sno != null ? sno.hashCode() : 0);
        result = 31 * result + age;
        return result;
    }
}
